package dao

import (
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"log"
	"react-blog-server/app/dao/model"
	"react-blog-server/config"
)

var DB *gorm.DB

func InitSqlite(cfg *config.Config) {
	dbConfig := &gorm.Config{}
	db, err := gorm.Open(sqlite.Open(cfg.Sqlite.DbPath), dbConfig)
	if err != nil {
		log.Fatalf("open config db failed, err: %v", err)
	}

	DB = db

	err = DB.AutoMigrate(
		&model.User{},
		&model.Category{},
	)

	if err != nil {
		log.Fatalf("自动生成表, err: %v", err)
	}
}
